package com.atguigu.ssyx.sys.service.impl;

import com.atguigu.ssyx.common.exception.SsyxException;
import com.atguigu.ssyx.common.result.ResultCodeEnum;
import com.atguigu.ssyx.model.sys.RegionWare;
import com.atguigu.ssyx.sys.mapper.RegionWareMapper;
import com.atguigu.ssyx.sys.service.RegionWareService;
import com.atguigu.ssyx.vo.sys.RegionWareQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 杨林
 * @create 2023-07-12 17:47 星期三
 * description:
 */
@Service
public class RegionWareServiceImpl extends ServiceImpl<RegionWareMapper, RegionWare> implements RegionWareService {

    @Autowired
    private RegionWareMapper regionWareMapper;

    /**
     * 获取开通区域列表
     * @param page
     * @param limit
     * @param regionWareQueryVo
     * @return
     */
    @Override
    public IPage index(Long page, Long limit, RegionWareQueryVo regionWareQueryVo) {
        //构建分页条件
        Page<RegionWare> queryPage = new Page<>(page, limit);
        //构建查询条件
        LambdaQueryWrapper<RegionWare> wrapper = new LambdaQueryWrapper<>();
        if (regionWareQueryVo.getKeyword() != null){
            wrapper.like(RegionWare::getRegionName,regionWareQueryVo.getKeyword());
        }
        Page<RegionWare> regionWarePage = regionWareMapper.selectPage(queryPage, wrapper);
        return regionWarePage;
    }

    /**
     * 根据id修改开通区域状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatusById(Long id, Integer status) {
        //根据id获取对应的RegionWare
        LambdaQueryWrapper<RegionWare> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RegionWare::getId,id);
        RegionWare regionWare = regionWareMapper.selectOne(wrapper);
        if (regionWare == null){
            throw new SsyxException(201,"未找到对应开通区域！");
        }
        regionWare.setStatus(status);
        regionWareMapper.updateById(regionWare);
    }
}
